package com.example.mystoryapp2.ui.user.regist

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.UserModel
import com.example.mystoryapp2.data.network.service.UserInfoService
import com.example.mystoryapp2.databinding.ActivityRegisterLayoutBinding
import com.example.mystoryapp2.domain.UserInfo
import com.example.mystoryapp2.enums.EnumRole
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.utils.DialogUtils
import com.example.mystoryapp2.ui.utils.UserInfoUtils
import retrofit2.Call
import retrofit2.Response

class RegisterActivity : BaseActivity<ActivityRegisterLayoutBinding, RegisterViewModel>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
    }

    override fun initView() {
        db.btnBack.setOnClickListener { back() }
        db.btnRegister.setOnClickListener { register() }
    }

    @SuppressLint("ResourceType")
    private fun initData() {
        val arrayList = ArrayList<String>()
        arrayList.add(EnumRole.READER.name)
        arrayList.add(EnumRole.AUTHOR.name)
        db.viewModel?.roleList = arrayList
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_register_layout
    }

    override fun getViewModel(): RegisterViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[RegisterViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }

    private fun register() {
        if (db.viewModel?.checkInputValid() == true) {
            ServiceCreator.create<UserInfoService>().register(
                UserInfo(
                    db.viewModel?.username?.value,
                    db.viewModel?.password?.value,
                    db.viewModel?.email?.value,
                    db.viewModel?.role,
                    db.viewModel?.bio?.value,
                    db.viewModel?.name?.value
                )
            ).enqueue(object : NetCallBack<String>() {
                override fun onResponse(call: Call<String>, response: Response<String>) {
                    if (TextUtils.isEmpty(response.body())) {
                        DialogUtils.showLongMessage(this@RegisterActivity,R.string.tip_failed)
                    } else {
                        AppContext.saveJwt(response.body())
                        db.viewModel?.loadUserinfo(object : UserModel.ICallBack {
                            override fun onLoaded() {
                                UserInfoUtils.afterLoginSuccess(
                                    AppContext.jwt,
                                    this@RegisterActivity
                                )
                            }

                            override fun onFailed() {
                                DialogUtils.showLongMessage(this@RegisterActivity,
                                    R.string.tip_failed)
                            }
                        })
                    }
                }
                override fun onFailure(call: Call<String>, t: Throwable) {
                    DialogUtils.showLongMessage(this@RegisterActivity,R.string.tip_failed)
                }
            })
        }
    }

    private fun back() {
        finish()
    }
}